package com.mi.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.mi.common.core.util.Result;
import com.mi.common.log.annotation.Log;
import com.mi.system.dto.query.DictItemQueryDTO;
import com.mi.system.dto.request.DictItemRequestDTO;
import com.mi.system.entity.DictItem;
import com.mi.system.service.DictItemService;
import com.mi.system.vo.DictItemBriefVO;
import com.mi.system.vo.DictItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

import static com.mi.common.core.util.Result.ok;

/**
 *  Dict item controller
 *
 *  @author     jin_huaquan
 *  @date      2021/11/1 15:05
 *  @version   1.0
 */
@Api(value = "字典项API", tags = "DictItem")
@ApiSort(994)
@RestController
@RequestMapping("/api/v1/dict/items")
@RequiredArgsConstructor
@Slf4j
public class DictItemController {

    private final DictItemService dictItemService;

    @ApiOperation(value = "分页查询字典项列表", notes = "分页查询字典项列表")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 1)
    @Log(title = "查询字典项列表")
    @GetMapping("/actions/query")
    public Result<IPage<DictItemVO>> listDictItem(Page page, DictItemQueryDTO query) {
        log.info("API CALL : query dict item.");

        return ok(dictItemService.listDictItem(page, query));
    }

    @ApiOperation(value = "查询字典项列表", notes = "查询字典项列表")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 2)
    @ApiImplicitParam(name = "name", value = "字典项的名称", paramType = "query", dataTypeClass = String.class, required = true)
    @Log(title = "罗列字典项")
    @GetMapping("/actions/list")
    public Result<List<DictItemBriefVO>> listDictItem(@NotBlank(message = "字典项类型不能为空") @RequestParam String type) {
        log.debug("API CALL : list dict item.");

        return ok(dictItemService.listDictItem(type));
    }

    @ApiOperation(value = "保存字典项", notes = "保存字典项")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 3)
    @Log(title = "新增字典项:#{#request.value}")
    @PostMapping
    public Result<Boolean> saveDictItem(@Valid @RequestBody DictItemRequestDTO request) {
        log.debug("API CALL : save dict item.");

        return ok(dictItemService.saveDictItem(request));
    }

    @ApiOperation(value = "更新字典项", notes = "更新字典项信息")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 4)
    @ApiImplicitParam(name = "id", value = "字典项的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "编辑字典项:#{#request.value}")
    @PutMapping("/{id}")
    public Result<Boolean> updateDictItem(@PathVariable Long id, @Valid @RequestBody DictItemRequestDTO request) {
        log.debug("API CALL : update dict item.");

        return ok(dictItemService.updateDictItem(id, request));
    }

    @ApiOperation(value = "删除字典项", notes = "删除字典项")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 5)
    @ApiImplicitParam(name = "id", value = "字典项的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "删除字典项:#{#request.value}")
    @DeleteMapping("/{id}")
    public Result<DictItem> removeDictItem(@PathVariable Long id) {
        log.debug("API CALL : remove dict item.");

        return ok(dictItemService.removeDictItem(id));
    }
}
